//
//  AppDelegate.m
//  BluetoothCameraHead
//
//  Created by wang on 16/11/1.
//  Copyright © 2016年 BG. All rights reserved.
//

#import "AppDelegate.h"
#import "BGAdViewController.h"
#import "BGMainTabBarController.h"
#import "BGBaseNavigationController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    NSArray *centralManagerIdentifiers = launchOptions[UIApplicationLaunchOptionsBluetoothCentralsKey];
    NSLog(@"%@",centralManagerIdentifiers);
    // 处理所有需要的初始化
    [self handleAllInitialize];
    
    // 设置根控制器
    [self setRootViewController:launchOptions];
    return YES;
}

/**处理所有需要的初始化*/
- (void)handleAllInitialize {
    
    
    // 4MB// 32MB
    NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:4*1024*1024 diskCapacity:32*1024*1024 diskPath:@"nsurlcache"];
    [NSURLCache setSharedURLCache:sharedCache];
    
    //    SDImageCache *sdCache = [SDImageCache sharedImageCache];
    //    [sdCache setMaxCacheAge:86400*10];//缓存10天
    //    [sdCache setMaxCacheSize:1024*1024*300];//缓存200M
    
    //统计工具
    //    [AppsFlyerTracker sharedTracker].appsFlyerDevKey = @"U3dGWiXuktz49PztHK5P7N";
    //    [AppsFlyerTracker sharedTracker].appleAppID = @"1031189956";
    
    //有盟
    //    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    //    [MobClick setAppVersion:version];
    //    [MobClick setEncryptEnabled:YES];//加密
    //    //#warning -mark 集成测试，正式上线要注释此代码
    //    //    [MobClick setLogEnabled:YES];//集成测试
    //    [MobClick startWithAppkey:@"571dba6967e58e4ec10000c4" reportPolicy:BATCH   channelId:@""];
    //
    //    // 初始化社交平台
    //    [[Twitter sharedInstance] startWithConsumerKey:@"EUIQtozlBVUxiWZeNhcWWRIgB" consumerSecret:@"QznnVByrWoY5TDNnWkbnYVzBfbqlhNaVPOmeUmBhUEMcfHQoeS"];
    //    [Fabric with:@[[Twitter sharedInstance]]];
    
    // 初始化语言设置
    [self initializeLanguage];
    
    
    // 注册根控制器切换的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shouldSwitchRootViewController:) name:BGSwitchRootViewControllerNotification object:nil];
}

/**初始化语言设置*/
- (void)initializeLanguage {
    // 没有保存过app语言
    if (!BGAppLanguage) {
        // 默认跟随系统
        [[NSUserDefaults standardUserDefaults] setObject:BGAppLanguageFollowSystem forKey:BGAppLanguageKey];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

///**切换根控制器的通知方法*/
- (void)shouldSwitchRootViewController:(NSNotification *)noti {
    BGMainTabBarController *mainViewController = [[BGMainTabBarController alloc] init];
    BGBaseNavigationController *nav = [[BGBaseNavigationController alloc] initWithRootViewController:mainViewController];
    //    if (noti.object != nil) {
    //        mainViewController.isUserFirst = [noti.object[@"isUserFirst"] boolValue];
    //    }
    [UIApplication sharedApplication].statusBarHidden = NO;
    [UIView transitionWithView:self.window duration:0.5f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        BOOL oldState = [UIView areAnimationsEnabled];
        [UIView setAnimationsEnabled:NO];
        self.window.rootViewController = nav;
        [UIView setAnimationsEnabled:oldState];
    } completion:NULL];
}

/**设置根控制器*/
- (void)setRootViewController:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    UIViewController *rootViewController = [[BGMainTabBarController alloc] init];
    self.window.rootViewController = rootViewController;
    [self.window makeKeyAndVisible];
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
